#include "departmentview.h"
#include "adminmanager.h"
#include <QHeaderView>
#include <QMessageBox>
#include <QInputDialog>
#include <QDebug>


DepartmentView::DepartmentView(QWidget *parent) :
    QWidget(parent)
{
    m_adminManager = new AdminManager(this);

    // 初始化UI组件
    m_departmentLabel = new QLabel("部门列表");
    m_employeeLabel = new QLabel("部门成员");

    // 部门表格
    m_departmentTable = new QTableWidget;
    m_departmentTable->setColumnCount(5);
    m_departmentTable->setHorizontalHeaderLabels({"部门ID", "部门名称", "部门经理ID", "部门经理姓名","部门人数"});
    m_departmentTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    connect(m_departmentTable, &QTableWidget::cellClicked, this, &DepartmentView::onDepartmentTableItemClicked);

    // 员工表格
    m_employeeTable = new QTableWidget;
    m_employeeTable->setColumnCount(4);
    m_employeeTable->setHorizontalHeaderLabels({"员工ID", "姓名", "职位", "入职日期"});
    m_employeeTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    // 添加部门控件
    m_deptNameLabel = new QLabel("部门名称:");
    m_deptNameEdit = new QLineEdit;
    m_managerIdLabel = new QLabel("经理ID:");
    m_managerIdEdit = new QLineEdit;

    m_addDepartmentButton = new QPushButton("添加部门");
    connect(m_addDepartmentButton, &QPushButton::clicked, this, &DepartmentView::onAddDepartmentButtonClicked);

    m_deleteDepartmentButton = new QPushButton("删除部门");
    connect(m_deleteDepartmentButton, &QPushButton::clicked, this, &DepartmentView::onDeleteDepartmentButtonClicked);

    m_backButton = new QPushButton("返回");
    connect(m_backButton, &QPushButton::clicked, this, &DepartmentView::onBackButtonClicked);

    // 设置布局
    QVBoxLayout *mainLayout = new QVBoxLayout;
    QHBoxLayout *buttonLayout = new QHBoxLayout;
    QHBoxLayout *addDeptLayout = new QHBoxLayout;

    addDeptLayout->addWidget(m_deptNameLabel);
    addDeptLayout->addWidget(m_deptNameEdit);
    addDeptLayout->addWidget(m_managerIdLabel);
    addDeptLayout->addWidget(m_managerIdEdit);
    addDeptLayout->addWidget(m_addDepartmentButton);

    buttonLayout->addWidget(m_deleteDepartmentButton);
    buttonLayout->addWidget(m_backButton);

    mainLayout->addLayout(addDeptLayout);
    mainLayout->addWidget(m_departmentLabel);
    mainLayout->addWidget(m_departmentTable);
    mainLayout->addWidget(m_employeeLabel);
    mainLayout->addWidget(m_employeeTable);
    mainLayout->addLayout(buttonLayout);

    setLayout(mainLayout);
    setWindowTitle("部门管理");
}

DepartmentView::~DepartmentView()
{
    // 释放UI组件
    delete m_departmentTable;
    delete m_employeeTable;
    delete m_addDepartmentButton;
    delete m_deleteDepartmentButton;
    delete m_backButton;
    delete m_departmentLabel;
    delete m_employeeLabel;
    delete m_deptNameEdit;
    delete m_managerIdEdit;
    delete m_deptNameLabel;
    delete m_managerIdLabel;
}

void DepartmentView::setAdminManager(AdminManager *manager)
{
    m_adminManager = manager;
    // 设置管理员后刷新部门列表
    if (m_adminManager)
    {
        refreshDepartmentList();
    }
}

void DepartmentView::refreshDepartmentList()
{
    if (!m_adminManager)
    {

        QMessageBox::warning(this, "警告", "未设置管理员管理器");
        return;
    }

    // 清空表格
    m_departmentTable->setRowCount(0);

    // 获取所有部门
    QList<QVariantList> departments = m_adminManager->getAllDepartments();

    // 填充表格
    for (int i = 0; i < departments.size(); ++i)
    {
        const QVariantList &deptData = departments[i];
        QVariantHash dept;
        dept.insert("dept_id", deptData[0]);
        dept.insert("dept_name", deptData[1]);
        dept.insert("dept_head_id", deptData[2]);
        dept.insert("emp_name", deptData[3]);
        dept.insert("dept_count", deptData[4]);
        m_departmentTable->insertRow(i);
        m_departmentTable->setItem(i, 0, new QTableWidgetItem(dept.value("dept_id").toString()));
        m_departmentTable->setItem(i, 1, new QTableWidgetItem(dept.value("dept_name").toString()));
        m_departmentTable->setItem(i, 2, new QTableWidgetItem(dept.value("dept_head_id").toString()));
        m_departmentTable->setItem(i, 3, new QTableWidgetItem(dept.value("emp_name").toString()));
        m_departmentTable->setItem(i, 4, new QTableWidgetItem(dept.value("dept_count").toString()));
       // m_departmentTable->setItem(i, 5, new QTableWidgetItem(dept.value("dept_name").toString()));


        // 根据实际的部门信息字段添加其他列
    }
}

void DepartmentView::refreshEmployeeList(int deptId)
{
    if (!m_adminManager)
    {
        QMessageBox::warning(this, "警告", "未设置管理员管理器");
        return;
    }

    // 清空表格
    m_employeeTable->setRowCount(0);

    // 获取部门员工
    QList<QVariantList> employees = m_adminManager->getDepartmentEmployees(deptId);

    // 填充表格
    for (int i = 0; i < employees.size(); ++i)
    {
        const QVariantList &emp = employees[i];
        m_employeeTable->insertRow(i);
        m_employeeTable->setItem(i, 0, new QTableWidgetItem(emp[0].toString()));
        m_employeeTable->setItem(i, 1, new QTableWidgetItem(emp[1].toString()));
        m_employeeTable->setItem(i, 2, new QTableWidgetItem(emp[2].toString()));
        m_employeeTable->setItem(i, 3, new QTableWidgetItem(emp[3].toString()));
    }
}

void DepartmentView::onAddDepartmentButtonClicked()
{
    if (!m_adminManager)
    {
        //QMessageBox::warning(this, "警告", "未设置管理员管理器");
        return;
    }

    QString deptName = m_deptNameEdit->text().trimmed();
    QString managerId = m_managerIdEdit->text().trimmed();

    if (deptName.isEmpty())
    {
        QMessageBox::warning(this, "警告", "部门名称不能为空");
        return;
    }

    if (managerId.isEmpty())
    {
        QMessageBox::warning(this, "警告", "经理ID不能为空");
        return;
    }

    bool success = m_adminManager->addDepartment(deptName, managerId);

    if (success)
    {
        QMessageBox::information(this, "成功", "部门添加成功");
        m_deptNameEdit->clear();
        m_managerIdEdit->clear();
        refreshDepartmentList();
    }
    else
    {
        QMessageBox::critical(this, "失败", "部门添加失败");
    }
}

void DepartmentView::onDeleteDepartmentButtonClicked()
{
    if (!m_adminManager)
    {
        QMessageBox::warning(this, "警告", "未设置管理员管理器");
        return;
    }

    int currentRow = m_departmentTable->currentRow();
    if (currentRow < 0)
    {
        QMessageBox::warning(this, "警告", "请选择要删除的部门");
        return;
    }

    int deptId = m_departmentTable->item(currentRow, 0)->text().toInt();
    QString deptName = m_departmentTable->item(currentRow, 1)->text();

    QMessageBox::StandardButton reply;
    reply = QMessageBox::question(this, "确认", QString("确定要删除部门 '%1' 吗?").arg(deptName),
                                  QMessageBox::Yes | QMessageBox::No);

    if (reply == QMessageBox::Yes)
    {
        bool success = m_adminManager->deleteDepartment(deptId);

        if (success)
        {
            QMessageBox::information(this, "成功", "部门删除成功");
            refreshDepartmentList();
            // 清空员工表格
            m_employeeTable->setRowCount(0);
            m_currentDeptId = -1;
        }
        else
        {
            QMessageBox::critical(this, "失败", "部门删除失败");
        }
    }
}

void DepartmentView::onDepartmentTableItemClicked(int row, int column)
{
    Q_UNUSED(column);

    int deptId = m_departmentTable->item(row, 0)->text().toInt();
    if (deptId != m_currentDeptId)
    {
        m_currentDeptId = deptId;
        refreshEmployeeList(deptId);
    }
}

void DepartmentView::onBackButtonClicked()
{
    // 发出返回信号，让主窗口处理
    emit backToMain();
}
